<script>
	import Vue from 'vue'
	export default {
		onLaunch: function() {
			uni.getSystemInfo({
				success: function(e) {
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif

					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					// #endif		

					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
				}
			})

			//用户登录
			this.authLogin()
		},
		onShow: function() {

		},
		onHide: function() {

		},
		methods: {
			authLogin() {
				uni.login({
					provider: 'weixin',
					success: async (loginRes) => {
						let code = loginRes.code

						this.$api.wechatLogin({
							code
						}).then(res => {
							this.$store.commit('user/setUid', res.uid)
							this.$store.commit('user/setUserInfo', res.userInfo)
							this.$store.commit('user/setLogin', true)
							if (res.token) {
								this.$store.commit('user/setToken', res.token, res.tokenExpired)
							}

							// 登录成功

							this.globalData.successCallbackHander.exec()
						})
					}
				})
			}
		},


		globalData: {
			//登录回调函数
			successCallbackHander: {
				callbackList: new Array(),
				//添加回调函数
				add(func, _this) {
					let isLogin = this.getState()
					let route = _this.__route__
					if (isLogin) {
						func()
					} else {
						let callIndex = this.callbackList.findIndex(call => call.route == route)
						if (callIndex === -1) {
							this.callbackList.push({
								func: func.bind(_this),
								route: route
							})
						}
					}
				},
				//执行回调函数
				exec() {
					this.callbackList.forEach(callback => callback.func())
				},
				getState: () => {
					return Vue.prototype.$store.getters['user/getLogin']
				}
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "./static/css/main.css";
	@import "./static/css/icon.css";
	@import "./static/css/animation.css";
	@import "./static/icon/iconfont.css";

	page {
		background-color: #FFFFFF;
	}
</style>
